//
//  IWillBorrowController.m
//  BrushFace
//
//  Created by timer_q on 15/12/11.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "IWillBorrowController.h"
#import "IWillHelper.h"
#import "NavHelper.h"
#import "ChooseDateView.h"
#import "LiXiView.h"
#import "BorrowScenceModel.h"
#import "AppStatus.h"
#import "BorrowingAgreementVC.h"
#import "ARRequestModel.h"
#import "HuanKuanCouponsViewController.h"
@interface IWillBorrowController ()<ChooseDateViewDelegate,LiXiViewDelegate,UITextFieldDelegate>
{
    BOOL _fabuSuccess;
    BOOL _reasonIn;

}
@property (nonatomic,strong)BorrowScenceModel *scenceModel;
@end

@implementation IWillBorrowController
{
    //借款理由
    __weak IBOutlet UILabel *reasonLabel;
    __weak IBOutlet UIImageView *reasonImageView;
    //借款日期
    __weak IBOutlet UILabel *timeLabel;
    ChooseDateView *_chooseView;
    //优惠券使用
    __weak IBOutlet UILabel *youhuiquanLabel;
    //选择金额
    __weak IBOutlet UIImageView *choose_firstImageView;
    __weak IBOutlet UILabel *choose_firstLabel;
    __weak IBOutlet UIImageView *choose_firstSelectImageView;
    
    __weak IBOutlet UILabel *choose_secondLabel;
    __weak IBOutlet UIImageView *choose_secondSelectImageView;
    __weak IBOutlet UIImageView *choose_secondImageView;

    __weak IBOutlet UILabel *choose_thirdLabel;
    __weak IBOutlet UIImageView *choose_thirdSelectImageView;
    __weak IBOutlet UIImageView *choose_thirdImageView;
    
    __weak IBOutlet UILabel *choose_lastLabel;
    __weak IBOutlet UIImageView *choose_LastImageView;
    __weak IBOutlet UIImageView *choose_lastSelectImageView;
    
    
    __weak IBOutlet UIView *chooseMoneyView;
    
    //利息及还款计划
    __weak IBOutlet UILabel *lixiLabel;
    __weak IBOutlet UILabel *systemMsgLabel;
    LiXiView *_lixiV;
    NSDictionary *_chooseDict;
    BOOL _isAgain;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self UIConfig];
    [self dataInit];
#pragma mark------xuxuejiao
    
    //改了两个字段，totalRecords
    
    [[ARRequestModel sharedInstance] youhuiquanCountAndCB:^(id output) {
        if ([output objectForKey:@"totalRecords"]) {
            
            youhuiquanLabel.text = [NSString stringWithFormat:@"优惠券"];
        }
        else
        {
            youhuiquanLabel.text = @"暂无可用优惠券";
        }
        
    }];
    
    [IWillHelper sharedInstance].reasonDetail = @"其他";

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    _fabuSuccess = NO;
    _reasonIn = YES;
    if ([IWillHelper sharedInstance].isRealName) {
        [_scenceModel hope_Savehope:^(NSString *hopeID) {
            [IWillHelper sharedInstance].hopeid = hopeID;
            [AppStatus sharedInstance].radarBackType = ENUM_BORROW_ROOT;
            [NavHelper gotoBorrowingColorVCWithHopeID:hopeID];
        }];
    }
    NSString *reason = [IWillHelper sharedInstance].reasonDetail;
    NSArray *reasonArray = @[
                             @{@"img":@"reason_zufang0",@"text":@"我要租房"},
                             @{@"img":@"reason_xuexi0",@"text":@"我要学习"},
                             @{@"img":@"reason_lvyou0",@"text":@"我要旅游"},
                             @{@"img":@"reason_shouji0",@"text":@"我要买手机"},
                             @{@"img":@"reason_lingyongqian0",@"text":@"我要生活费"},
                             @{@"img":@"reason_zhouzhuan0",@"text":@"暂时周转"},
                             @{@"img":@"otherreason",@"text":reason}];
    reasonImageView.image = [UIImage imageNamed:[[reasonArray objectAtIndex:[IWillHelper sharedInstance].reasonID] objectForKey:@"img"]];
    reasonLabel.text = [[reasonArray objectAtIndex:[IWillHelper sharedInstance].reasonID] objectForKey:@"text"];
//    if (_isAgain == YES) {
//        _isAgain = NO;
//        NSDictionary *againDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"AGAIN"];
//        [self chooseMoneyButtonClick:self.changeBtn];
//        NSString *money = [NSString stringWithFormat:@"%@",againDict[@"amount"]];
//        self.textfield.text = money;
//        [IWillHelper sharedInstance].amount = money;
//        [IWillHelper sharedInstance].reasonID = 6;
//        [IWillHelper sharedInstance].reasonDetail = [NSString stringWithFormat:@"%@",againDict[@"reason"]];
//        reasonLabel.text = [IWillHelper sharedInstance].reasonDetail;
//        [IWillHelper sharedInstance].couponsid = [NSString stringWithFormat:@"%@",againDict[@"hoperate"]];
//        [IWillHelper sharedInstance].days = [NSString stringWithFormat:@"%@",againDict[@"days"]];
//        [IWillHelper sharedInstance].priod = [NSString stringWithFormat:@"%@",againDict[@"priod"]];
//        if ([[IWillHelper sharedInstance].priod integerValue]==0) {
//            timeLabel.text = [NSString stringWithFormat:@"%@天",[IWillHelper sharedInstance].days];
//        }else{
//            timeLabel.text = [NSString stringWithFormat:@"%@个月",[IWillHelper sharedInstance].priod];
//        }
//        [self lixiChangeLixi:[IWillHelper sharedInstance].couponsid andMoney:money];
//
//    }
    
}
- (void)againBorrow{
    _isAgain = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 界面
- (void)UIConfig{
    [self.navBar setTitle:@"我要借钱"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    
    _chooseView = [[ChooseDateView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _chooseView.hidden = YES;
    _chooseView.delegate = self;
    [self.view addSubview:_chooseView];
    
    _lixiV = [[LiXiView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _lixiV.hidden = YES;
    _lixiV.delegate = self;
    [self.view addSubview:_lixiV];
    
    self.textfield.delegate = self;
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 60, 44)];
    [rightBtn setTitle:@"协议" forState:UIControlStateNormal];
#pragma mark --xuxuejiao
    //“协议”字号改为12
    
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [rightBtn addTarget:self action:@selector(gotoJiekuanXieYi) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.navBar setRightView:rightBtn];
}
- (void)gotoJiekuanXieYi{
    BorrowingAgreementVC *vc = [[BorrowingAgreementVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 数据
- (void)dataInit{
    _scenceModel = [BorrowScenceModel SceneModel];
    [_scenceModel hope_getHopeCreation:^{
        ITLogSP(@"成功获取token");
    }];
    NSArray *labelArray = @[choose_firstLabel,choose_secondLabel,choose_thirdLabel,choose_lastLabel];
    NSArray *imageArray = @[choose_firstImageView,choose_secondImageView,choose_thirdImageView,choose_LastImageView];
    NSArray *selectImageArray = @[choose_firstSelectImageView,choose_secondSelectImageView,choose_thirdSelectImageView,choose_lastSelectImageView];
    _chooseDict = @{@"label":labelArray,@"img":imageArray,@"selectImg":selectImageArray};
    [IWillHelper helperChooseMoneyButtonClickWithIndex:0 fromDict:_chooseDict];
    [IWillHelper sharedInstance].days = @"0";
    [IWillHelper sharedInstance].priod = @"1";
    
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
    
    [IWillHelper sharedInstance].isRealName = NO;
}

#pragma mark - someAction
//借款理由
- (IBAction)reasonButtonClick:(id)sender {
    if (_reasonIn == YES) {
        _reasonIn = NO;
        [NavHelper gotoBorrowReasonVC];
    }
}
//借款期限
- (IBAction)timeButtonClick:(id)sender {
    [_chooseView chooseViewWillShow];
    _chooseView.hidden = NO;
}
//优惠券选择
- (IBAction)youhuiquanButtonClick:(id)sender {
    HuanKuanCouponsViewController *vc = [[HuanKuanCouponsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
//马上用钱
- (IBAction)OKButtonClick:(id)sender {
    
    if (_fabuSuccess == YES) {
        return;
    }
    _fabuSuccess = YES;
    if (self.changeBtn.selected == YES) {
        NSString *moneyStr = _textfield.text;
        if ([moneyStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0||![moneyStr isMoneyWithMillion]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入100-100万之间的整数钱数" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            _fabuSuccess = NO;
            return;
        }else{
            [IWillHelper sharedInstance].amount = self.textfield.text;
        }
    }
    
    [_scenceModel common_fullnamed:^(BOOL isFull) {
        if (isFull == YES) {
            [_scenceModel hope_Savehope:^(NSString *hopeID) {
                [IWillHelper sharedInstance].hopeid = hopeID;
                [AppStatus sharedInstance].radarBackType = ENUM_BORROW_ROOT;
               [NavHelper gotoBorrowingColorVCWithHopeID:hopeID];
                
            }];
        }else{
            [NavHelper gotoRealNameVCWithType:0];
        }
    }];
    
    
}
//选择金额
- (IBAction)chooseFirstMoneyButtonClick:(id)sender {
    [IWillHelper helperChooseMoneyButtonClickWithIndex:0 fromDict:_chooseDict];
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
}
- (IBAction)chooseSecondMoneyButtonClick:(id)sender {
    [IWillHelper helperChooseMoneyButtonClickWithIndex:1 fromDict:_chooseDict];
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
}
- (IBAction)chooseThirdMoneyButtonClick:(id)sender {
    [IWillHelper helperChooseMoneyButtonClickWithIndex:2 fromDict:_chooseDict];
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
}
- (IBAction)chooseLastMoneyButtonCLick:(id)sender {
    [IWillHelper helperChooseMoneyButtonClickWithIndex:3 fromDict:_chooseDict];
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
}
- (IBAction)chooseMoneyButtonClick:(UIButton *)sender {
    if (sender.selected == YES) {
        sender.selected = NO;
        self.textFieldBackView.hidden = YES;
        chooseMoneyView.hidden = NO;
    }else{
        self.textFieldBackView.hidden = NO;
        chooseMoneyView.hidden = YES;
        sender.selected = YES;
        
    }
}
//利息及还款计划
- (IBAction)tiaozhenglixiButtonClick:(id)sender {
    [_lixiV lixiShow];
    _lixiV.hidden = NO;
}
- (IBAction)huankuanjihuaButtonClick:(id)sender {
    [NavHelper gotoBorrowHuanKuanPlanVC];
}

#pragma mark - chooseDelegate
- (void)chooseDate:(NSString *)date andProid:(NSString *)proid andDays:(NSString *)days{
    [IWillHelper sharedInstance].days = days;
    [IWillHelper sharedInstance].priod = proid;
    
    NSInteger priod = [[IWillHelper sharedInstance].priod integerValue];
    if (priod<=1) {
        if (priod==1) {
            [IWillHelper sharedInstance].couponsid=@"8";
        }else{
            if ([[IWillHelper sharedInstance].days integerValue]<=10) {
                [IWillHelper sharedInstance].couponsid=@"10";
            }else{
                [IWillHelper sharedInstance].couponsid=@"8";
            }
            
        }
    }else if ([[IWillHelper sharedInstance].priod integerValue]>1&&[[IWillHelper sharedInstance].priod integerValue]<=3) {
        [IWillHelper sharedInstance].couponsid=@"6";
    }else if ([[IWillHelper sharedInstance].priod integerValue]>3&&[[IWillHelper sharedInstance].priod integerValue]<=6) {
        [IWillHelper sharedInstance].couponsid=@"5";
    }else if ([[IWillHelper sharedInstance].priod integerValue]>6&&[[IWillHelper sharedInstance].priod integerValue]<=12) {
        [IWillHelper sharedInstance].couponsid=@"4";
    }

    
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
    timeLabel.text = date;
}
- (void)lixiChangeLixi:(NSString *)lixi andMoney:(NSString *)money{
    lixiLabel.text = money;
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    NSLog(@"%@",textField.text);
    [IWillHelper sharedInstance].amount = textField.text;
    lixiLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoney]];
    return YES;
}
@end
